package com.rong.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rong.ruiji.common.BaseContext;
import com.rong.ruiji.common.R;
import com.rong.ruiji.dto.DishDto;
import com.rong.ruiji.entity.Category;
import com.rong.ruiji.entity.Dish;
import com.rong.ruiji.entity.DishFlavor;
import com.rong.ruiji.service.CategoryService;
import com.rong.ruiji.service.DishFlavorService;
import com.rong.ruiji.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService ;

    @Autowired
    private CategoryService categoryService ;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param request
     * @param dishDto
     * @return
     */
     @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody DishDto dishDto) {
         Long emp = (Long) request.getSession().getAttribute("employee");
         dishService.saveWithFlavor(emp,dishDto);
         String key = dishDto.getCategoryId()+""+dishDto.getStatus();
         redisTemplate.delete(key);
         return R.success("新增菜品成功");
     }

     @GetMapping("/page")
    public R<Page> page(@RequestParam int page,@RequestParam int pageSize,@RequestParam(required = false) String name) {
         //构造分页构造器
         Page<Dish> pageInfo = new Page<>(page,pageSize);
         Page<DishDto> dishDtoPage = new Page<>();
         //构造条件构造器
         LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
         queryWrapper.like(name != null,Dish::getName,name);
         //添加排序条件
         queryWrapper.orderByDesc(Dish::getId);
         //执行查询
         dishService.page(pageInfo,queryWrapper);

         //对象拷贝
         BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
         List<Dish> records = pageInfo.getRecords();
         List<DishDto> list = records.stream().map((itme)->{
             DishDto dishDto = new DishDto();
             BeanUtils.copyProperties(itme,dishDto);
             Long categoryId = itme.getCategoryId();
             Category category = categoryService.getById(categoryId);
             String categoryName = category.getName();
             dishDto.setCategoryName(categoryName);

             return dishDto;
         }).collect(Collectors.toList());

         dishDtoPage.setRecords(list);
         return R.success(dishDtoPage);
     }

    /**
     * 根据id查询菜品和口味信息
     * @param id
     * @return
     */
     @GetMapping("/{id}")
     public R<DishDto>  getDish(@PathVariable Long id) {
         DishDto dishDto = dishService.getByIdWithFlavor(id);
         return R.success(dishDto);
     }

    /**
     * 修改菜品
     * @param request
     * @param dishDto
     * @return
     */
     @PutMapping
     public R<String> update(HttpServletRequest request,@RequestBody DishDto dishDto) {
         Long emp =(Long) request.getSession().getAttribute("employee");
         dishService.updateWithFlavor(emp,dishDto);
         String key = dishDto.getCategoryId()+""+dishDto.getStatus();
         redisTemplate.delete(key);
         return R.success("修改成功");
     }

     @GetMapping("/list")
    public R<List<Dish>> getDishByCategory(@RequestParam Long categoryId,@RequestParam(required = false) Integer status) {
         String key =""+categoryId+status;
         List<Dish> list = (List<Dish>) redisTemplate.opsForValue().get(key);

         if(list != null) {
             return R.success(list);
         }

         LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();

         if(status != null) {
             queryWrapper.eq(Dish::getStatus,status);
             queryWrapper.eq(Dish::getCategoryId,categoryId);
             List<Dish> dishList =  dishService.list(queryWrapper);
             redisTemplate.opsForValue().set(key,dishList,60, TimeUnit.MINUTES);
             return R.success(dishList);
         }

         //状态为1，表示正在销售的菜品
         queryWrapper.eq(Dish::getStatus,1);
         queryWrapper.eq(Dish::getCategoryId,categoryId);
          List<Dish> dishList =  dishService.list(queryWrapper);
          redisTemplate.opsForValue().set(key,dishList,60, TimeUnit.MINUTES);
          return R.success(dishList);
     }


}
